//Francisco "Kiko" Olvera
//sets up directx
//DirectX.h

#include "DirectX.h"
#include "Definitions.h"

//initialize directx and direct input
void DirectXFramework::Init(HWND hwnd, HINSTANCE hInst)
{
	//direct3d
	pD3D = Direct3DCreate9(D3D_SDK_VERSION);

	ZeroMemory( &d3dpp, sizeof(d3dpp) );

	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
	d3dpp.BackBufferCount  = 1;
	d3dpp.BackBufferHeight = SCREEN_HEIGHT;
	d3dpp.BackBufferWidth  = SCREEN_WIDTH;
	d3dpp.hDeviceWindow    = hwnd;

	d3dpp.EnableAutoDepthStencil		= TRUE;
	d3dpp.AutoDepthStencilFormat		= D3DFMT_D16;
	
	SetWindowLong(d3dpp.hDeviceWindow, GWL_STYLE, WS_OVERLAPPEDWINDOW);
	SetWindowPos(d3dpp.hDeviceWindow, HWND_TOP, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
	
	pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice);
    
	pd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(255, 255, 255));
	pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, TRUE);
	pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
	pd3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
	pd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE); 


	//pd3dDevice->SetRenderState(D3DRS_FOGENABLE, TRUE);
	//pd3dDevice->SetRenderState(D3DRS_FOGCOLOR,0x00FFFFFF); //fog color to white
	//pd3dDevice->SetRenderState(D3DRS_FOGTABLEMODE,D3DFOG_LINEAR); //pixel calcs done on graphics card
	//pd3dDevice->SetRenderState(D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR);//vertex calcs done by DX (more cpu intensive)
	////float fFogDensity = 0.5f;
	////pd3dDevice->SetRenderState(D3DRS_FOGDENSITY, *((DWORD*)(&fFogDensity)));	//exponential calculations
	//
	//float Start   = 0.5f;    // For linear mode
 //   float End     = 0.8f;
	//pd3dDevice->SetRenderState(D3DRS_FOGSTART,  *(DWORD *)(&Start));	//linear calculations
	//pd3dDevice->SetRenderState(D3DRS_FOGEND,  *(DWORD *)(&End));	//linear calculations
	////D3DRS_RANGEFOGENABLE


	//camera
	cam.createCamera(1.0f, 1200.0f);	// near clip plane, far clip plane
}

//re-aquire keyboard and mouse for direct input
void DirectXFramework::Update()
{
	cam.Update();
}

void DirectXFramework::RenderBegin()
{
	// Clear the backbuffer to a black color
	pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0 );

	pd3dDevice->BeginScene();
}

void DirectXFramework::RenderEnd()
{
	pd3dDevice->EndScene();

	// Present the backbuffer contents to the display
	pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

//cleanup
void DirectXFramework::Release()
{
	pd3dDevice->Release();
	pD3D->Release();
}


void DirectXFramework::moveCameraLeft()
{
	cam.moveCameraLeft();
}

void DirectXFramework::moveCameraRight()
{
	cam.moveCameraRight();
}

void DirectXFramework::toggleCameraUp()
{
	cam.toggleCameraUp();
}